from selenium.webdriver import Chrome,ChromeOptions
from selenium.webdriver.common.keys import Keys
from time import sleep
from lxml import etree


options = ChromeOptions()
# 不让发现是自动的
options.add_argument('--disable-blink-features=AutomationControlled')
# 设置无头模式
options.add_argument('--headless')
options.add_argument('--disable-gpu')
# 拿到Chrome对象
web = Chrome(options=options)
web.get('https://www.douyin.com/')
search_input = web.find_element_by_xpath('//*[@id="root"]/div/div[1]/div/header/div[2]/div/div[2]/div/form/input[4]')
# 输入内容并确定
search_input.send_keys('美女', Keys.ENTER)
# 切换到最后一个窗口
web.switch_to.window(web.window_handles[-1])

tree = etree.HTML(web.page_source)
li_list = tree.xpath('//*[@id="root"]/div/div[2]/div/div[2]/div[2]/ul/li[1]/div')
print(li_list.text)

# for li in li_list:
#     print(li.xpath('./div/a'))

# li_list = web.find_element_by_xpath('//*[@id="root"]/div/div[2]/div/div[2]/div[2]/ul/li[1]')
# li_list.send_keys(Keys.ENTER)
#
# print(li_list)
# for li in li_list:
#     print(li.find_element_by_xpath('//a').get_attribute('href'))
# sleep(5)
# web.close()

